unit JATUIF_Component_Menu;

{$mode objfpc}{$H+}

interface

uses
    Classes, Contnrs, SysUtils, Video, JATUIF_Component, JATUIF_Component_Button, JATUIF_Driver_Screen;

Type

    { TMenu }

    TMenuItemCallback = Procedure(ID:Integer) of Object;

    TMenu = class(TComponent)
      Protected
        Procedure _OnKeyPress(Key:Char); override;
        Procedure _OnMouseEnter; override;
        Procedure _OnMouseLeave; override;
        Procedure _OnClick; override;
        Procedure _OnLoseFocus; override;
        Procedure Draw; override;
        Procedure SetVisible(Vis:Boolean); override;
        Procedure _DummyMenuClick(ID:Integer);
        Procedure _ItemClicked;
      Private
        OnMenuClick:TMenuItemCallback;
        Buttons:TObjectList;
        Button_Tags:TStringList;
        Button_IDs:TStringList;
      Public
        Constructor Create(Parent:TScreen; X1,Y1,X2,Y2:Integer);
        Procedure AddItem(Caption:String; ID:Integer);
        Procedure Show;
        Property OnItemClick : TMenuItemCallback write OnMenuClick;
    end;

implementation

{ TMenu }

Constructor TMenu.Create(Parent: TScreen; X1, Y1, X2, Y2: Integer);
begin
     OnMenuClick:= @_DummyMenuClick;
     Buttons:= TObjectList.create(true);
     Button_Tags:= TStringList.Create;
     Button_IDs:= TStringList.Create;
     inherited Create(Parent, X1, Y1, X2, Y2);
     Self.Visible:= false;
end;

procedure TMenu.AddItem(Caption: String; ID: Integer);
begin
     If Length(Caption)+1 > (sX2-sX1) then begin
        While Length(Caption)+1 > (sX2-sX1) do begin
            SetLength(Caption,Length(Caption)-1);
        end;
        SetLength(Caption,Length(Caption)-2);
        Caption:=Caption+'..';
     end;
     Buttons.Add(TButton.Create(Screen,sX1,sY1+Buttons.Count,Caption,Black,White,Red,White));
     (Buttons[Buttons.Count-1] as TButton).Visible:=false;
     Button_Tags.Add(IntToStr((Buttons[Buttons.Count-1] as TButton).GetTag));
     Button_IDs.Add(IntToStr(ID));
     (Buttons[Buttons.Count-1] as TButton).OnClick:=@_ItemClicked;
     (Buttons[Buttons.Count-1] as TButton).OnMouseEnter:=@_OnMouseEnter;
     (Buttons[Buttons.Count-1] as TButton).OnMouseLeave:=@_OnMouseLeave;
end;

procedure TMenu.Draw;
var
   i,n:integer;
begin
     for i:=sY1 to sY2 do begin
         for n:=sX1 to sX2 do begin
             screen.DrawCharXY(GetTag, n, i, ' ', white, white);
         end;
     end;
     { Draw Component }
end;

procedure TMenu.SetVisible(Vis: Boolean);
Var
   i:integer;

begin
     If Buttons.Count > 0 then begin
        For i:=0 to Buttons.Count-1 do begin
            (Buttons[i] as TButton).Visible:=Vis;
        end;
     end;
     inherited SetVisible(Vis);
end;

procedure TMenu._DummyMenuClick(ID: Integer);
begin
     //Do Nothing
end;

procedure TMenu._ItemClicked;
Var
   Index:integer;

begin
     Index:= Button_Tags.IndexOf(IntToStr(Screen.GetLastClicked));
     If Index <> -1 then OnMenuClick(StrToInt(Button_IDs[Index]));
     Self.Visible:=False;
end;

procedure TMenu.Show;
Var
   i:Integer;

begin
     Self.BringToFront;
     If Buttons.Count > 0 then begin
        For i:=0 to Buttons.Count-1 do begin
            (Buttons[i] as TButton).BringToFront;
        end;
     end;
     Self.Visible:=True;
end;

{ Callback Events }

procedure TMenu._OnKeyPress(Key: Char);
begin
     inherited _OnKeyPress(Key);
end;

procedure TMenu._OnMouseEnter;
begin
     self.Visible:=True;
     inherited _OnMouseEnter;
end;

procedure TMenu._OnMouseLeave;
begin
     self.Visible:=False;
     inherited _OnMouseLeave;
end;

procedure TMenu._OnClick;
begin
     inherited _OnClick;
end;

procedure TMenu._OnLoseFocus;
begin
     inherited _OnLoseFocus;
end;

end.

